package com.toommi.refuse.controller;

import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ..
 * 地址级联
 *
 * @author yangg
 * @since 2019-07-24 16:33
 */
@RestController
@RequestMapping(value = "/api/common")
@Slf4j
@AllArgsConstructor
@Api(tags = "行政区划、街道接口")
public class AreaController {

    private AreaService areaService;

    /**
     * 获取下级地址
     *
     * @param parent 父级ID
     * @return 地址集合
     */
    @ApiOperation(value = "获取下级地址", notes = "获取下级地址")
    @ApiImplicitParam(name = "parent", value = "父级ID，没有顶级则不传", required = true, dataType = "String")
    @RequestMapping(value = "/areas", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse getAreas(@RequestParam String parent) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(areaService.findAreasByParent(parent));
        } catch (Exception e) {
            log.error("get ares error :{}", e);
            commonResponse = CommonResponse.serverError();
        }

        return commonResponse;
    }

}
